package com.cristina.attention.aspect;

import com.alibaba.fastjson.JSON;
import com.cristina.attention.annotation.SyncChat;
import com.cristina.attention.domain.Chat;
import com.cristina.attention.publisher.SyncChatPublisher;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author by superman
 */
@Component
@Aspect
public class SyncChatAspect {

    /**
     * 配置环绕通知, apiLog上注册的切入点
     *
     * @param point join point for advice
     * @param syncChat 切入点
     * @return
     * @throws Throwable
     */
    @Around("@annotation(syncChat)")
    public Object around(ProceedingJoinPoint point, SyncChat syncChat) throws Throwable {
        Object result = point.proceed();
        //获取方法参数值数组
        Object[] args = point.getArgs();
        Chat chat = JSON.parseObject(JSON.toJSONString(args[1]), Chat.class);
        SyncChatPublisher.publishEvent(chat, syncChat);
        return result;
    }

    /**
     * 配置异常通知
     *
     * @param joinPoint join point for advice
     * @param e exception
     * @param syncChat 切入点
     */
    @AfterThrowing(pointcut = "@annotation(syncChat)", throwing = "e")
    public void logAfterThrowing(JoinPoint joinPoint, Throwable e, SyncChat syncChat) {
        // 异常处理
    }
}
